package org.weixin4j.miniprogram.component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.weixin4j.miniprogram.Configuration;
import org.weixin4j.miniprogram.WeixinException;
import org.weixin4j.miniprogram.WeixinMiniprogram;
import org.weixin4j.miniprogram.http.HttpsClient;
import org.weixin4j.miniprogram.http.Response;
import org.weixin4j.miniprogram.model.pay.*;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;

/**
 * @author wangzhengwei
 * @create 2021/12/29
 */
public class PayComponent extends AbstractComponent {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public PayComponent(WeixinMiniprogram weixinMiniprogram) {
        super(weixinMiniprogram);
    }

    /**
     * 统一下单
     *
     * @param unifiedorder 统一下单对象
     * @return 下单返回结果对象
     */
    public UnifiedOrderResult payUnifiedOrder(UnifiedOrder unifiedorder) throws WeixinException {
        //将统一下单对象转成XML
        String xmlPost = unifiedorder.toXML();
        if (Configuration.isDebug()) {
            System.out.println("调试模式_统一下单接口 提交XML数据：" + xmlPost);
        }
        //创建请求对象
        HttpsClient http = new HttpsClient();
        //提交xml格式数据
        Response res = http.postXml("https://api.mch.weixin.qq.com/pay/unifiedorder", xmlPost);
        //获取微信平台下单接口返回数据
        String xmlResult = res.asString();
        try {
            JAXBContext context = JAXBContext.newInstance(UnifiedOrderResult.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            UnifiedOrderResult result = (UnifiedOrderResult) unmarshaller.unmarshal(new StringReader(xmlResult));
            return result;
        } catch (JAXBException ex) {
            logger.error("统一下单失败:{}", ex);
            return null;
        }
    }

    /**
     * 服务商统一下单
     *
     * @param unifiedOrderSL 统一下单对象
     * @return 下单返回结果对象
     */
    public UnifiedOrderSLResult payUnifiedOrderSL(UnifiedOrderSL unifiedOrderSL) throws WeixinException {
        //将统一下单对象转成XML
        String xmlPost = unifiedOrderSL.toXML();
        if (Configuration.isDebug()) {
            System.out.println("调试模式_服务商统一下单接口 提交XML数据：" + xmlPost);
        }
        //创建请求对象
        HttpsClient http = new HttpsClient();
        //提交xml格式数据
        Response res = http.postXml("https://api.mch.weixin.qq.com/pay/unifiedorder", xmlPost);
        //获取微信平台下单接口返回数据
        String xmlResult = res.asString();
        try {
            JAXBContext context = JAXBContext.newInstance(UnifiedOrderSLResult.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            UnifiedOrderSLResult result = (UnifiedOrderSLResult) unmarshaller.unmarshal(new StringReader(xmlResult));
            return result;
        } catch (JAXBException ex) {
            return null;
        }
    }

    /**
     * 查询订单
     *
     * @param orderQuery 订单查询对象
     * @return 订单查询结果
     */
    public OrderQueryResult payOrderQuery(OrderQuery orderQuery) throws WeixinException {
        //将统一下单对象转成XML
        String xmlPost = orderQuery.toXML();
        if (Configuration.isDebug()) {
            System.out.println("调试模式_查询订单接口 提交XML数据：" + xmlPost);
        }
        //创建请求对象
        HttpsClient http = new HttpsClient();
        //提交xml格式数据
        Response res = http.postXml("https://api.mch.weixin.qq.com/pay/orderquery", xmlPost);
        //获取微信平台查询订单接口返回数据
        String xmlResult = res.asString();
        try {
            if (Configuration.isDebug()) {
                System.out.println("调试模式_查询订单接口 接收XML数据：" + xmlResult);
            }
            JAXBContext context = JAXBContext.newInstance(OrderQueryResult.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            OrderQueryResult result = (OrderQueryResult) unmarshaller.unmarshal(new StringReader(xmlResult));
            return result;
        } catch (JAXBException ex) {
            return null;

        }

    }

}
